Apache ActiveMQ ™ -- JMS and JDBC operations in one transaction
Using ActiveMQ > User Submitted Configurations > JMS and JDBC operations in one transaction
JMS and JDBC operations in one transaction with Spring/Jencks/ActiveMQ
Spring beans:
Java code:
public class EchoBean implements MessageListener {
private Log log = LogFactory.getLog(getClass());
private JdbcTemplate jdbcTemplate;
private JmsTemplate jmsTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void onMessage(Message message) {
log.debug(message);
if (message instanceof TextMessage) {
try {
String messageText = ((TextMessage)message).getText();
log.debug("execute JMS operation");
jmsTemplate.convertAndSend("messages.ouptut", messageText);
log.debug("execute JDBC operation");
jdbcTemplate.execute("insert into t1 values('"+messageText+"')");
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}